package com.gec.system.controller;


import com.gec.model.entity.SysMenu;
import com.gec.model.vo.AssginMenuVo;
import com.gec.system.service.SysMenuService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author lys
 * @since 2023-11-24
 */
@Api(tags = "菜单管理控制器")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询总菜单")
    @GetMapping("/findMenuTree")
    public Result findMenuTree() {
        List<SysMenu> sysMenuList = this.sysMenuService.findMenuTree();
        return Result.ok(sysMenuList);
    }

    @ApiOperation("添加菜单节点")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody SysMenu sysMenu) {

        boolean save = this.sysMenuService.save(sysMenu);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/getMenuById/{id}")
    @ApiOperation("根据id获取节点信息")
    public Result getMenuById(@PathVariable Long id) {
        SysMenu sysMenu = this.sysMenuService.getById(id);
        return Result.ok(sysMenu);

    }

    @ApiOperation("修改节点信息")
    @PostMapping("/updateMenu")
    public Result updateMenu(@RequestBody SysMenu sysMenu) {

        boolean update = this.sysMenuService.updateById(sysMenu);
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("通过id删除节点")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@PathVariable Long id) {
        boolean b = this.sysMenuService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("根据id获取角色的菜单权限")
    @GetMapping("/toAssign/{id}")
    public Result toAssign(@PathVariable Long id) {
        List<SysMenu> list = this.sysMenuService.findSysMenuByRoleId(id);
        return Result.ok(list);
    }

    @ApiOperation("保存角色 菜单权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {

        this.sysMenuService.saveRoleAuth(assginMenuVo);
        return Result.ok();
    }

}

